package com.apanal.qlife.personal.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.advice.service.SysAnnounceService;
import com.apanal.qlife.advice.service.SysInformService;
import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.org.service.OrganizationService;
import com.apanal.qlife.sys.model.User;

@Controller
@RequestMapping("/personal")
public class MessageCenterController {

	@Autowired
	private SysAnnounceService sysAnnounceservice;

	@Autowired
	private SysInformService sysInformService;

	@Autowired
	private OrganizationService orgService;

	/**
	 * 查询所有公告消息
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "个人消息管理", operation = "分页查询通知", type = LogType.SELECT)
	// @RequiresPermissions("mycenter:message:view")
	@RequestMapping(value = "messageList2")
	public String messageList2(@CurrentUser User user, Model model,
			Advice advice, Page page, HttpServletRequest request) {
		Organization org = new Organization();
		org.setOrgId(user.getOrgId());
		Organization orgz = orgService.findOrganizationById(org);
		String orgPIds = orgz.getOrgPIds() + orgz.getOrgId().toString();
		org.setOrgPIds(orgPIds);
		PageReturn<Advice> pr = sysAnnounceservice
				.findAdvice(advice, org, page);
		model.addAttribute("aviceTitle", advice.getAdvTitle());
		model.addAttribute("result", pr);
		System.out.println("=============" + advice.getAdvTitle());
		return "personal/messageAnnouceList";
	}

	// 查找所有通知消息
	@SystemLog(module = "个人消息管理", operation = "分页查询公告", type = LogType.SELECT)
	// @RequiresPermissions("mycenter:message:view")
	@RequestMapping(value = "messageList")
	public String messageList(@CurrentUser User user, Model model,
			Advice advice, Page page, HttpServletRequest request) {
		Organization org = new Organization();
		org.setOrgId(user.getOrgId());
		Organization orgz = orgService.findOrganizationById(org);
		String orgPIds = orgz.getOrgPIds() + orgz.getOrgId().toString();
		org.setOrgPIds(orgPIds);
		PageReturn<Advice> pr = sysInformService.findInform(advice, page, org);
		model.addAttribute("result", pr);
		model.addAttribute("aviceTitle", advice.getAdvTitle());
		System.out.println("=============2" + advice.getAdvTitle());
		return "personal/messageInformList";
	}

	// 查看通知详细信息
	// @RequiresPermissions("mycenter:message:view")
	@RequestMapping(value = "/{advId}/massegeDetail")
	public String massegeDetail(Model model, Advice advice, Page page,
			HttpServletRequest request) {

		Advice ad = sysInformService.findInformDetailById(advice.getAdvId());
		model.addAttribute("advice", ad);
		return "personal/messageDetail";

	}

	// 查看公告详细信息
	// @RequiresPermissions("mycenter:message:view")
	@RequestMapping(value = "/{advId}/massegeDetailAnnouce")
	public String massegeDetailAnnouce(Model model, Advice advice, Page page,
			HttpServletRequest request) {
		Advice ad = sysInformService.findInformDetailById(advice.getAdvId());
		model.addAttribute("advice", ad);
		return "personal/messageDetail";

	}

}
